package com.example.qiniutest.aop.removeAop;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.qiniutest.aop.mapper.RemoveMapper;
import lombok.extern.log4j.Log4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

/**
 * @program: qiniuTest
 * @description: controller删除方法remove的aop类(再删除前做状态判断)
 * @author: xpp011
 * @create: 2021-10-27 13:29
 **/

@Component
@Aspect
@Log4j
public class RemoveAop {

    @Pointcut("execution(public com.example.qiniutest.util.R com.example.qiniutest.aop.controller.*Controller.remove(java.util.List) )")
    public void removeTarget() {

    }

    @Autowired
    RemoveMapper removeMapper;

    @Around("removeTarget()")
    public void before(ProceedingJoinPoint pj) throws Throwable {
        Object[] args = pj.getArgs();
        //获取删除的ids
        List<Long> ids = ((List<Long>) args[0]);
        Class<?> aClass = pj.getTarget().getClass();
        //获取代理类的方法
        Method[] methods = aClass.getDeclaredMethods();

        for (Method method : methods) {
            //查找插入方法   只为得到实体类得到table名称
            if ("insert".equals(method.getName())) {
                //得到insert方法参数
                Class<?>[] parameterTypes = method.getParameterTypes();
                Class<?> parameterType = parameterTypes[0];
                log.info("实体类名" + parameterType.getName());


                TableName annotation = null;
                while (parameterType!=Object.class && annotation == null) {
                    //得到注解TableName
                    annotation=parameterType.getAnnotation(TableName.class);
                    parameterType=parameterType.getSuperclass();
                }
                //无法得到table名称  该方法无效
                if (annotation==null) continue;



                //得到注解TableName的value值
                String tableName = annotation.value();
                log.info("表名" + tableName);



                try {
                    QueryWrapper<?> queryWrapper = new QueryWrapper<>();
                    queryWrapper.in("id", ids);
                    //查询status字段
                    List<Integer> status = removeMapper.selectStatus(tableName, queryWrapper);
                    for (Integer i : status) {
                        //判断是否有处理中1  或者已经通过9 的数据
                        if (i != null && (i == 1 || i == 9)) {
                            throw new RuntimeException("有数据处于正在处理中或者以通过，无法删除");
                        }
                    }
                } catch (Exception e) {
                    //可能抛出异常  如没有status列
                    e.printStackTrace();
                    //这边做直接返回，不做对不符合id做过滤
                    return;
                }
            }
        }

        //只有数据不处于1或者9才执行方法
/*      修改执行参数
        ids.addAll(Arrays.asList(999L,888L,777L));
        pj.proceed(new Object[]{ids});*/
        pj.proceed();
    }
}
